﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.NetworkInformation;
using NETCONLib;
using System.ServiceProcess;
using System.Diagnostics;
using System.IO;

namespace SetUpWi_Fi
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private string _ssidName;//缓存需要启动的wi-fi 名
        public MainWindow()
        {
            InitializeComponent();
            LoadData();

        }
        void LoadData()
        {
            string[] network = ComSubHelper.GetLocalNetworkInformation();
            cbx_Network.ItemsSource = network;
            cbx_Network.SelectedIndex = 0;
        }
        //承载网络模式设置为允许 netsh wlan set hostednetwork mode=allow
        //设置一个名字为tangWi-Fi的无线接入点，接入点密码为123123123 netsh wlan set hostednetwork ssid=tangWi-Fi key=123123123
        //启动承载网络 netsh wlan start hostednetwork
        private void btn_setUpWiFi_Click(object sender, RoutedEventArgs e)
        {
            tb_messageInfo.Text = "";
            _ssidName = txt_wifiName.Text.Trim();
            SetWiFi(txt_wifiName.Text.Trim(), txt_wifipwd.Text.Trim());
            ComSubHelper.ShareNetwork(cbx_Network.Text, _ssidName);

        }

        private void SetWiFi(string wifiName, string wifiPwd)
        {
            string hostednetwork = string.Format("netsh wlan set hostednetwork mode={0}", "allow");
            string wlan = string.Format("netsh wlan set hostednetwork ssid={0} key={1}", wifiName, wifiPwd);
            string startwlan = "netsh wlan start hostednetwork";
            //string cmd = ComSubHelper.ExecuteCmd(hostednetwork);
            //cmd += ComSubHelper.ExecuteCmd(wlan);
            //cmd += ComSubHelper.ExecuteCmd(startwlan);
            string[] cmd = new string[] { hostednetwork, wlan, startwlan };
            string cmdstr = ComSubHelper.ExecuteCmd(cmd);
            tb_messageInfo.Text = cmdstr;
        }

        private void btn_setDownWiFi_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
